package com.h2cloud.copy;

import org.springframework.beans.BeanUtils;

/**
 * @author: wulongkai
 * @create: 2022-11-01 10:47
 **/
public class test {
    public static void main(String[] args) {
//        SourceBean sourceBean = new SourceBean(1, "Bob");
//        TargetBean targetBean = new TargetBean();
//        BeanUtils.copyProperties(sourceBean, targetBean);
//        System.out.println("targetBean = " + targetBean);

        // 浅拷贝
//        SourceBean sourceBean = new SourceBean();
//        Map<String, String> sourceContent = new HashMap<>();
//        sourceContent.put("name", "Bob");
//        sourceBean.setContent(sourceContent);
//        TargetBean targetBean = new TargetBean();
//        BeanUtils.copyProperties(sourceBean, targetBean);
//        // targetBean内容
//        System.out.println(targetBean.getContent().get("name"));
//        // 只修改sourceBean内容
//        sourceBean.getContent().put("name", "Peter");
//        // targetBean内容同时被修改
//        System.out.println(targetBean.getContent().get("name"));

//        SourceBean sourceBean = new SourceBean();
//        sourceBean.setIds(Arrays.asList(1, 2, 3));
//        sourceBean.setBaseInfo(new BaseInfo("longkai"));
//        TargetBean targetBean = new TargetBean();
//        BeanUtils.copyProperties(sourceBean, targetBean);
//        System.out.println("targetBean = " + targetBean);
//        String firstId = targetBean.getIds().get(0); // 运行报错


        CopyTest1 test1 = new CopyTest1();
        test1.outerName = "hahaha";
        CopyTest1.InnerClass innerClass = new CopyTest1.InnerClass();
        innerClass.InnerName = "hohoho";
        test1.innerClass = innerClass;
        System.out.println(test1.toString());

        CopyTest2 test2 = new CopyTest2();
        test2.innerClass = new CopyTest2.InnerClass();
        BeanUtils.copyProperties(test1, test2);
        BeanUtils.copyProperties(test1.innerClass, test2.innerClass);

        System.out.println(test2.toString());
    }
}
